<template>
  <div class="hotel-home">
    <!-- 顶部导航栏 -->
    <el-header class="header">
      <div class="logo">Cloudscape Hotel</div>
    </el-header>

    <!-- Banner区域 -->
    <div class="banner">
      <div class="banner-content">
        <h1>欢迎来到云端酒店</h1>
        <p>云端之上，不止是风景 —— 云端酒店，为您托起每一段轻盈时光。</p>
        <el-button type="primary" size="large" @click="scrollToMessages" >立即留言</el-button>
      </div>
    </div>

    <!-- 服务亮点 -->
    <div class="services">
      <h2 class="section-title">我们的服务</h2>
      <el-row :gutter="30">
        <el-col :span="6" v-for="(service, index) in services" :key="index">
          <el-card class="service-card">
            <div class="icon-container">
              <i :class="service.icon" class="icon"></i>
            </div>
            <h3>{{ service.title }}</h3>
            <p>{{ service.description }}</p>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 留言管理区域 -->
    <div class="messages" ref="messagesSection">
      <h2 class="section-title">客户留言</h2>
      
      <!-- 留言表单 -->
      <el-card class="message-form">
        <el-form :model="messageForm" :rules="messageRules" ref="messageFormRef">
          <el-form-item prop="content">
            <el-input
              type="textarea"
              :rows="4"
              placeholder="请输入您的留言..."
              v-model="messageForm.content"
              resize="none"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitMessage" :loading="submitting">提交留言</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- 留言列表 -->
      <div class="message-list">
        <el-card v-for="(message) in messages" :key="message.id" class="message-item">
          <div class="message-header">
          <span class="message-author">{{ getUserName(message.userId) }}</span>
            <span class="message-date">{{ formatDate(message.createTime) }}</span>
          </div>
          <div class="message-content">{{ message.content }}</div>
          
          <!-- 回复内容 -->
          <div v-if="message.replyContent" class="reply-container">
            <div class="reply-header">
              <span class="reply-author">酒店回复</span>
              <span class="reply-date">{{ formatDate(message.replyTime) }}</span>
            </div>
            <div class="reply-content">{{ message.replyContent }}</div>
          </div>
        </el-card>
      </div>
    </div>

    <!-- 底部区域 -->
    <!-- <el-footer class="footer">
      <div class="footer-content">
        <div class="contact-info">
          <h3>联系我们</h3>
          <p><i class="el-icon-location"></i> 北京市朝阳区王府井大街88号</p>
          <p><i class="el-icon-phone"></i> 400-888-9999</p>
          <p><i class="el-icon-message"></i> contact@royalpalace.com</p>
        </div>
        <div class="copyright">
          <p>© 2025 皇家宫殿酒店 版权所有</p>
        </div>
      </div>
    </el-footer> -->
  </div>
</template>

<script>
import { listMessage, addMessage } from '@/api/message.js'
import { listUser } from '@/api/User.js'
import { getUserId } from '@/utils/auth.js'

export default {
  name: 'HomePage',
  data() {
    return {
       userMap: {}, // 分类映射（userId -> name）
      activeIndex: '1',
      services: [
        {
          icon: 'el-icon-house',
          title: '豪华客房',
          description: '精心设计的各类房型，满足您的不同需求'
        },
        {
          icon: 'el-icon-food',
          title: '精致餐饮',
          description: '专业负责的厨师团队，为您呈现美食盛宴'
        },
        {
          icon: 'el-icon-place',
          title: '会议场地',
          description: '专业会议设施，适合各类商务活动'
        },
        {
          icon: 'el-icon-service',
          title: '尊享服务',
          description: '24小时管家服务，为您提供无微不至的关怀'
        }
      ],
      messages: [], // 留言列表
      messageForm: {
        content: ''
      },
      messageRules: {
        content: [
          { required: true, message: '请输入留言内容', trigger: 'blur' },
          { min: 5, message: '留言内容至少5个字符', trigger: 'blur' },
          { max: 500, message: '留言内容最多500个字符', trigger: 'blur' }
        ]
      },
      submitting: false // 提交状态
    }
  },
  mounted() {
    this.loadMessages()
    // 获取用户id->name映射信息
    this.loadUser()
  },
  methods: {
    // 根据分类ID获取分类名称
    getUserName(userId) {
      return this.userMap[userId] || '游客'
    },
    async loadUser() {
      try {
        const response = await listUser()
        if (response.code === 200) {
          response.rows.forEach(user => {
            this.userMap[user.id] = user.username
          })
        }
      } catch (error) {
        console.error('获取分类失败:', error)
      }
      
    },
    // 加载留言列表
    async loadMessages() {
      try {
        const response = await listMessage()
        if (response.code === 200) {
          this.messages = response.rows
        } else {
          this.$message.error('留言加载失败：' + response.msg)
        }
      } catch (error) {
        console.error('加载留言时出错:', error)
        this.$message.error('留言加载失败，请稍后再试')
      }
    },
    
    // 提交留言
    async submitMessage() {
      this.$refs.messageFormRef.validate(async (valid) => {
        if (valid) {
          this.submitting = true
          try {
            // 获取当前用户ID（可能为空）
            const userIdRes =  getUserId()
            const userId = userIdRes || null
            
            const messageData = {
              content: this.messageForm.content,
              userId: userId
            }
            
            const response = await addMessage(messageData)
            if (response.code === 200) {
              this.$message.success('留言提交成功！')
              this.messageForm.content = ''
              this.loadMessages() // 重新加载留言列表
            } else {
              this.$message.error('留言提交失败：' + response.msg)
            }
          } catch (error) {
            console.error('提交留言时出错:', error)
            this.$message.error('留言提交失败，请稍后再试')
          } finally {
            this.submitting = false
          }
        } else {
          return false
        }
      })
    },
    
    // 格式化日期
    formatDate(dateString) {
      if (!dateString) return ''
      const date = new Date(dateString)
      return date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
      })
    },
    
    // 滚动到留言区域
    scrollToMessages() {
      this.$refs.messagesSection.scrollIntoView({ behavior: 'smooth' })
    }
  }
}
</script>

<style scoped>
.hotel-home {
  font-family: 'Helvetica Neue', Arial, sans-serif;
  color: #333;
}

/* 头部样式 */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #545c64;
  color: #fff;
  padding: 0 20px;
  height: 60px;
}

.logo {
  font-size: 24px;
  font-weight: bold;
  letter-spacing: 1px;
}

/* Banner样式 */
.banner {
  height: 500px;
  background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), 
              url('https://images.unsplash.com/photo-1566073771259-6a8506099945?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1800&q=80');
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: white;
}

.banner-content {
  max-width: 800px;
  padding: 20px;
}

.banner h1 {
  font-size: 48px;
  margin-bottom: 20px;
  text-shadow: 1px 1px 3px rgba(0,0,0,0.8);
}

.banner p {
  font-size: 24px;
  margin-bottom: 30px;
}

/* 分区标题 */
.section-title {
  text-align: center;
  font-size: 32px;
  margin: 50px 0 30px;
  color: #2c3e50;
  position: relative;
}

.section-title::after {
  content: '';
  display: block;
  width: 80px;
  height: 3px;
  background: #409EFF;
  margin: 15px auto;
}

/* 服务区域 */
.services {
  padding: 40px 20px;
  background-color: #f8f9fa;
}

.service-card {
  height: 220px;
  text-align: center;
  transition: transform 0.3s;
  margin-bottom: 20px;
}

.service-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 16px rgba(0,0,0,0.1);
}

.icon-container {
  width: 60px;
  height: 60px;
  margin: 0 auto 15px;
  background: #e6f7ff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon {
  font-size: 28px;
  color: #409EFF;
}

.service-card h3 {
  color: #2c3e50;
  margin-bottom: 10px;
}

.service-card p {
  color: #666;
  font-size: 14px;
}

/* 留言区域 */
.messages {
  padding: 40px 20px;
  max-width: 1000px;
  margin: 0 auto;
}

.message-form {
  margin-bottom: 40px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

/* 留言列表 */
.message-list {
  max-height: 600px;
  overflow-y: auto;
  padding-right: 10px;
}

.message-item {
  margin-bottom: 20px;
  border-left: 3px solid #409EFF;
  transition: all 0.3s;
}

.message-item:hover {
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  transform: translateX(5px);
}

.message-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  font-size: 14px;
}

.message-author {
  font-weight: bold;
  color: #409EFF;
}

.message-date {
  color: #999;
}

.message-content {
  line-height: 1.6;
  color: #333;
}

/* 回复区域 */
.reply-container {
  margin-top: 15px;
  padding: 15px;
  background-color: #f5f7fa;
  border-radius: 4px;
  border-left: 2px solid #67C23A;
}

.reply-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 14px;
}

.reply-author {
  font-weight: bold;
  color: #67C23A;
}

.reply-date {
  color: #999;
}

.reply-content {
  color: #555;
  line-height: 1.6;
}

/* 底部样式 */
.footer {
  background-color: #2c3e50;
  color: #ecf0f1;
  padding: 30px 0 20px;
}

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.contact-info {
  text-align: center;
  margin-bottom: 20px;
}

.contact-info h3 {
  font-size: 20px;
  margin-bottom: 15px;
}

.contact-info p {
  margin: 5px 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.contact-info i {
  margin-right: 8px;
  font-size: 16px;
}

.copyright {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid rgba(255,255,255,0.1);
  width: 100%;
  font-size: 14px;
  color: #95a5a6;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header {
    flex-direction: column;
    height: auto;
    padding: 10px;
  }
  
  .logo {
    margin-bottom: 10px;
  }
  
  .banner h1 {
    font-size: 36px;
  }
  
  .banner p {
    font-size: 18px;
  }
  
  .services .el-col {
    margin-bottom: 20px;
  }
  
  .message-form {
    padding: 15px;
  }
}
</style>